/* 
 *      Real Time Clock Driver Test/Example Program 
 * 
 *      Compile with: 
 *           gcc -s -Wall -Wstrict-prototypes rtctest.c -o rtctest 
 * 
 *      Copyright (C) 1996, Paul Gortmaker. 
 * 
 *      Released under the GNU General Public License, version 2, 
 *      included herein by reference. 
 * 
 */  
  
#include <stdio.h>   
#include <linux/rtc.h>   
#include <sys/ioctl.h>   
#include <sys/time.h>   
#include <sys/types.h>   
#include <fcntl.h>   
#include <unistd.h>   
#include <stdlib.h>   
#include <errno.h>   
  
#define RTC_ALM_PWRON_SET       _IOW('p', 0x15, struct rtc_time) /* Set alarm time  */
  
/* 
 * This expects the new RTC class driver framework, working with 
 * clocks that will often not be clones of what the PC-AT had. 
 * Use the command line to specify another RTC if you need one. 
 */  
static const char default_rtc[] = "/dev/rtc0";  
  
  
int main(int argc, char **argv)  
{  

    struct rtc_time rtc_tm;  
    const char *rtc = default_rtc;  
    int ret, fd;
    fd = open(rtc, O_RDONLY);  
    ret = ioctl(fd, RTC_RD_TIME, &rtc_tm);  
    if (ret == -1) {  
        perror("RTC_RD_TIME ioctl");  
        exit(errno);  
    }  
  
    fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",  
        rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,  
        rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);  
  
    /* Set the alarm to 5 sec in the future, and check for rollover */  
    rtc_tm.tm_sec += 5;  
    rtc_tm.tm_min += 2;
    if (rtc_tm.tm_sec >= 60) {  
        rtc_tm.tm_sec %= 60;  
        rtc_tm.tm_min++;  
    }  
    if (rtc_tm.tm_min == 60) {  
        rtc_tm.tm_min = 0;  
        rtc_tm.tm_hour++;  
    }  
    if (rtc_tm.tm_hour == 24)  
        rtc_tm.tm_hour = 0;  
  
    ret = ioctl(fd, RTC_ALM_PWRON_SET, &rtc_tm);  
    if (ret == -1) {  
        if (errno == ENOTTY) {  
            fprintf(stderr,  
                "\n...Alarm IRQs not supported.\n");  
        }  
        perror("RTC_ALM_PWRON_SET ioctl");  
        exit(errno);  
    }  	
	close(fd);
    return 0;  
}  



